package com.example.lib_net

import com.example.lib_net.interceptor.HttpHeaderInterceptor
import com.example.lib_net.interceptor.SignInterceptor
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
import javax.inject.Qualifier
import javax.inject.Singleton


@Module
@InstallIn(SingletonComponent::class)
class NetModule {


@Singleton
@Provides
@CommonOkHttpClient
fun provideOkhttpClient(): OkHttpClient {
    val interceptor = HttpLoggingInterceptor()
    //HttpLoggingInterceptor()
    if (true) {
        interceptor.level = okhttp3.logging.HttpLoggingInterceptor.Level.BODY
    }
    return OkHttpClient.Builder()
        .writeTimeout(60, TimeUnit.SECONDS)
        .readTimeout(60, TimeUnit.SECONDS)
        .connectTimeout(60, TimeUnit.SECONDS)
        .addInterceptor(HttpLoggingInterceptor())
        .addInterceptor(interceptor)
        .addInterceptor(HttpHeaderInterceptor())
        .build()
}


    @Singleton
    @Provides
    @SignOkHttpClient
    fun provideSignOkhttpClient(): OkHttpClient {
        val interceptor = HttpLoggingInterceptor()
        if (true) {
            interceptor.level = okhttp3.logging.HttpLoggingInterceptor.Level.BODY
        }
        return OkHttpClient.Builder()
            .writeTimeout(60, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .connectTimeout(60, TimeUnit.SECONDS)
            .addInterceptor(SignInterceptor())
            .addInterceptor(interceptor)
            .addInterceptor(HttpHeaderInterceptor())
            .build()
    }

    @Provides
    @Singleton
    @CommonRetrofit
    fun provideRetrofit(@CommonOkHttpClient client:OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl("http://10.161.9.80:8886")
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(client)
            .build()
    }

    @Provides
    @Singleton
    @TokenRetrofit
    fun provideTokenRetrofit(@TokenOkHttpClient client:OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl("http://10.161.9.80:8886")
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
    }


    /******限定符******///为了区分一样的网络请求（不区分的话，他不知道具体是哪个），所以使用@Qualifier
    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class CommonOkHttpClient

    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class TokenOkHttpClient

    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class SignOkHttpClient

    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class CommonRetrofit

    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class TokenRetrofit

    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class SignRetrofit

}